#ifndef PARSE_BASIC_H__
#define PARSE_BASIC_H__

#include <stdlib.h>
#include "token.h"
#include "../ast/basic.h"

#if 0
import std.lib;
import token;
import ..ast.basic;
#endif

struct CharacterLiteral* parseCharacterLiteral(struct TokenList** source)
{
	if(*source == NULL) return NULL;

	if((*source)->head->tag == CHARACTER_LITERAL_TOKEN)
	{
		struct CharacterLiteral* r = malloc(sizeof(struct CharacterLiteral));
		r->character = *((*source)->head->characters);
		*source = (*source)->tail;
		return r;
	}

	return NULL;
}

struct IntegerLiteral* parseIntegerLiteral(struct TokenList** source)
{
	if(*source == NULL) return NULL;

	struct TokenList* save = *source;
	struct IntegerLiteral* r = malloc(sizeof(struct IntegerLiteral));

	if((*source)->head->tag == INTEGER_LITERAL_TOKEN)
	{
		r->integer = 0;

		char* pointer = (*source)->head->characters;

		while(*pointer != '\0')
		{
			r->integer *= 10;
			r->integer += *pointer - '0';
			pointer++;
		}

		*source = (*source)->tail;
		return r;
	}

	free(r);
	*source = save;
	return NULL;
}

struct Name* parseName(struct TokenList** source)
{
	if(*source == NULL) return NULL;

	struct Name* r = malloc(sizeof(struct Name));

	if((*source)->head->tag == NAME_TOKEN)
	{
		r->characters = (*source)->head->characters;
		*source = (*source)->tail;
		return r;
	}

	free(r);
	return NULL;
}

struct StringLiteral* parseStringLiteral(struct TokenList** source)
{
	if(*source == NULL) return NULL;

	if((*source)->head->tag == STRING_LITERAL_TOKEN)
	{
		struct StringLiteral* r = malloc(sizeof(struct StringLiteral));
		r->characters = (*source)->head->characters;
		*source = (*source)->tail;
		return r;
	}

	return NULL;
}

#endif
